import vuex from "vuex";
import Vue from "vue";
import { getAuth } from "api/interface.js";
import dynamic from "router/dynamicRoutes";
import allRoutes from "router/allRouter";
import recursion from "utils/recursionRoutes";

Vue.use(vuex);

export default new vuex.Store({
  // strict: true,
  state: {
    sides: [],
    brandCumb: [],
    roleName: null,
    info: {}
  },
  mutations: {
    RAISE_SIDE(state, poyload) {
      state.sides = [...poyload];
      /* 找到home路由 */
      const home = dynamic.find((item) => item.name === "home");
      /* 保存到state */
      /* 添加子路由 */
      home && (home.children = state.sides);
      /* 追加到页面上 */
    },
    CLEAR_SIDE(state) {
      state.sides = []
    },
    RAISE_BRAND(state, poyload) {
      state.brandCumb = poyload
    },
    INJECT_ROLENAME(state, poyload) {
      state.roleName = poyload
    },
    ASSIN_INFO(state) {
      state.info = Vue.prototype.$save.takeOut('INFO')
    }
  },
  actions: {
    async DIS_SIDE({ commit }) {
      /* 获取权限列表 */
      const result = await getAuth();
      /* 对比权限可选项 */
      const authRoutes = recursion(result?.data?.menuList || [], allRoutes);
      commit("RAISE_SIDE", authRoutes);
      commit('INJECT_ROLENAME', result.data.roleName)
      commit('ASSIN_INFO')
    },
  },
});
